<?php


namespace App\Model;


use EasySwoole\ORM\DbManager;

class WeighbridgeListModel extends \EasySwoole\ORM\AbstractModel
{
    protected $connectionName = 'default';
    protected $tableName = "psw_pounds_list";

    public function getWeighbridgeList($par,$field = '*'){
        return DbManager::getInstance()->invoke(function ($client) use ($par,$field){
            $m = self::invoke($client);
            $m->field($field);

            $where = [];
            //磅单号模糊查询
//            $serialNumber = $par['serial_number'];
            if ($par['serial_number'] && $par['serial_number'] !=''){
                $where['serial_number'] = ["%{$par['serial_number']}%", 'like'];
            }
            //车牌号模糊查询
            $licensePlate = $par['license_plate'];
            if ($licensePlate && $licensePlate != ''){
                $where['license_plate'] = ["%$licensePlate%", 'like'];
            }
            //货名模糊查询
            $cargoName = $par['cargo_name'];
            if ($cargoName && $cargoName != ''){
                $where['cargo_name'] = ["%$cargoName%", 'like'];
            }
            //关联运单筛选
            $associatedWaybill = $par['associated_waybill'];  //-1  1 2
            if ($associatedWaybill && $associatedWaybill != ''&& $associatedWaybill != -1){
                $where['type'] = ["$associatedWaybill", '='];
            }
            //磅单时间筛选
            $startTime = $par['start_time'];
            $endTime = $par['end_time'];
            if($startTime && $endTime){
                $where["(weighing_time >='{$startTime} 00:00:00' and weighing_time<='{$endTime} 23:59:59')"] = [];//时间
            }
            if ($where != ''){
                $m->where($where);
            }


//            //分页初始化
//            $par['pn'] = isset($par['pn']) && $par['pn']?$par['pn']:0;  //页码
//            $par['page_size'] = isset($par['page_size']) && $par['page_size']?$par['page_size']:0;  //每页条数

            $m->withTotalCount();

            //分页限制
            $m->limit((($par['pn'] - 1) * $par['page_size']),$par['page_size']);


            //设置默认排序规则
            $sorter_field = 'suttle';
            $sorter_order = 'desc';

            //当有参数传递时的排序规则
            if ($par['sorter_field'] && $par['sorter_field'] != ''){
                $sorter_field = $par['sorter_field'];
            }

            if ($par['sorter_order'] && $par['sorter_order'] != '') {
                if ($par['sorter_order'] == 'ascend'){
                    $sorter_order = 'asc';
                }else{
                    $sorter_order = 'desc';
                }
            }
            $m->order($sorter_field,$sorter_order);

            $m->all();

            //打印最后的SQL语句
            print_r("打印最后的SQL语句:" . $m->lastQuery()->getLastQuery());
            echo PHP_EOL;

            $weighbridgelist['data'] =  $m->lastQueryResult()->getResult();
            $weighbridgelist['total'] =  $m->lastQueryResult()->getTotalCount();

//            echo '得到数据数' . var_dump($weighbridgelist['total']);
//            var_dump("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//            var_dump($weighbridgelist['data'] . PHP_EOL);
            return $weighbridgelist;
        });
    }
}